<?php 
/**
 * 前台商品列表页控制器
 */
class ListController extends CommonController{
	
	public function index(){
		//处理左侧分类导航(面包屑)
		//获得get里面的cid
		$cid = Q('get.cid',0,'intval');
		$data = K('Category')->all();
		//获得父级分类
		$fatCate = $this->getFather($data, $cid);
		//数组反转
		$fatCate = array_reverse($fatCate);
		$this->assign('fatCate',$fatCate);
		
		//处理右侧分类导航
		//获得该分类的子级分类
		$sonCate = K('Category')->where("pid={$cid}")->all();	
		//判断如果没有子分类就不进行跳转操作
		if(!$sonCate){
			//获得该分类的pid
			$pid = K('Category')->where("cid={$cid}")->getField('pid',true);			
			$pid = $pid[0];			
			//找到所有该pid下的子集分类
			$sonCate = K('Category')->where("pid={$pid}")->all();
		}		
		$this->assign('sonCate',$sonCate);
		
		//处理底部导航
		$topCate = K('Category')->where('pid=0')->all();
		$this->assign('topCate',$topCate);

		//处理筛选
		//获得当前分类的子集cid
		$cids = $this->_getSon($cid);
		//获得该子集分类下的所有商品id
		$gids = K('Shop')->where("category_cid in(" . implode(',', $cids) . ")")->getField('sid',true);
		//如果分类下面有商品的时候
		if($gids){
			//通过商品gid得到属性
			$attr = K('ShopAttr')->where("shop_sid in(" . implode(',', $gids) . ")")->group('savalue')->all();
			//以下两个临时数组为重组列表页需要的属性
			$tempAttr = array();
			foreach ($attr as $v) {
				$tempAttr[$v['type_attr_ta_id']][] = $v;
			}
			//定义一个空数组用来获得该分类下所有商品的商品属性
			$tempFinalAttr = array();
			//组合有名称name的属性
			foreach ($tempAttr as $taid => $value) {
				$tempFinalAttr[] = array(
					'name' => K('TypeAttr')->where("ta_id=" . $taid)->getField('ta_name'),
					'value' => $value
				);
			}
			$this->assign('tempFinalAttr',$tempFinalAttr);

			$num = count($tempFinalAttr);
			$param = isset($_GET['param']) ? explode('_', $_GET['param']) : array_fill(0, $num, 0);
			$this->assign('param',$param);
			//进行筛选
			foreach ($param as $v) {
				if($v){
					$filterGids[] = M()->join('__shop_attr__ s1 JOIN __shop_attr__ s2 ON s1.savalue=s2.savalue')->where("s1.said={$v}")->getField('s2.shop_sid',true);
				}
			}		
			//如果有筛选的sid
			if(isset($filterGids)){
				//取交集
				$finalGids = $filterGids[0];
				foreach ($filterGids as $v) {
					$finalGids = array_intersect($finalGids, $v);
				}
				//属性获得交集gid之后，再和分类下面的商品gid再取交集
				$finalGids = array_intersect($finalGids,$gids);
				
			}else{
				//全部不限的时候，就是当前分类下面的所有商品gid
				$finalGids = $gids;
			}
			$finalGids = (implode(',', $finalGids));
			//通过筛选后的商品id查询商品信息
			$shops = K('Shop')->where("sid in($finalGids)")->all();
			$this->assign('shops',$shops);
		}else{
			//如果分类下面没有商品,把gid赋值为空数组
			$finalGids = array();
		}
		
	    $this->display();
	}

	//获得子集分类
	private function _getSon($cid){
		$data = K('Category')->all();
		$cids = $this->_getSonCid($data,$cid);
		$cids[] = $cid;
		return $cids;
	}
	
	//获得子集分类cid
	private function _getSonCid($data,$cid){
		$temp = array();
		foreach ($data as $v) {
			if($v['pid'] == $cid){
				$temp[] = $v['cid'];
				$temp = array_merge($temp,$this->_getSonCid($data, $v['cid']));
			}
		}
		return $temp;
	}
		
	//获得父级分类	
	private function getFather($data,$cid){
		static $temp = array();
		foreach ($data as $v) {
			if($v['cid'] == $cid){
				$temp[] = $v;
				$this->getFather($data, $v['pid']);
			}
		}
		return $temp;
	}
}



 ?>